package com.cai.dao.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.cai.dao.DbUtil;
import com.cai.dao.UserDao;
import com.cai.pojo.DetailedTopic;
import com.cai.pojo.DetailedUser;
import com.cai.pojo.UserInfo;
import com.cai.pojo.UserLogin;
import com.cai.util.C3p0Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author RuoLi
 * 方法实现类
 */
public class UserDaoImpl implements UserDao {
    DbUtil util = new DbUtil();

    @Override
    public int userLogin(String userAccount, String userPassword) {
        int count = -1;
        String sql = "SELECT count(*) FROM user_login WHERE login_account = ? AND login_password = ?";
        String[] params = {userAccount, userPassword};
        if (DbUtil.selectCountIsExist(sql, params) != 0) {
            return DbUtil.selectCountIsExist(sql, params);
        }
        return count;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<UserLogin> verifyIdentity(String userAccount, String userPassword) {
        String sql = "SELECT * FROM user_login WHERE login_account = ? AND login_password = ?";
        String[] params = {userAccount, userPassword};
        return (List<UserLogin>) util.selectContent(sql, params, UserLogin.class);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<UserLogin> verifyUserInfo(int userId) {
        String sql = "SELECT login_id,login_account,login_identity FROM user_login WHERE login_id = ?";
        String[] params = {String.valueOf(userId)};
        return (List<UserLogin>) util.selectContent(sql, params, UserInfo.class);
    }

    @Override
    public int insertUser(UserLogin userLogin) {
        int count = -1;
        //获取账号当前最大值
        int newAccount = Integer.parseInt(getUserMaxAccount()) + 1;
        String sql = "INSERT INTO `online_quiz_website`.`user_login`(`login_account`, `login_password`, `login_Mail`,`login_identity`) VALUES (?, ?, ?, ?)";
        String[] params = {String.valueOf(newAccount), userLogin.getLogin_password(), userLogin.getLogin_Mail(), "PersonType1"};
        count = DbUtil.updateContent(sql, params);
        if (count != -1) {
            return newAccount;
        }
        return count;
    }

    @Override
    public String getUserMaxAccount() {
        DbUtil dbUtil = new DbUtil();
        String sql = "SELECT MAX(login_account) FROM user_login";
        return dbUtil.selectContent(sql);
    }

    @Override
    public int updateUserPassword(String userAccount, String userMail, String newPassword) {
        int count = -1;
        String sql = "UPDATE`user_login` SET `login_password` = ? WHERE `login_Mail` = ? AND `login_account` = ?";
        Object[] params = {newPassword, userMail, userAccount};
        count = DbUtil.updateContent(sql, params);
        return count;
    }

    @Override
    public int showUserAccount(int userId) {
        DbUtil dbUtil = new DbUtil();
        String sql = "select login_account from user_login where login_id=?";
        String[] params = {String.valueOf(userId)};
        if (dbUtil.selectContent(sql, params) != null) {
            return Integer.parseInt(dbUtil.selectContent(sql, params));
        }
        return -1;
    }

    @Override
    @SuppressWarnings("unchecked")
    public String getUserData(int currentPage, int pageSize) {
        String sql = "CALL show_page_tables_DetailedUser(?,?)";
        List<DetailedUser> list = (List<DetailedUser>) DbUtil.getUserInfoPageData(sql, currentPage, pageSize);
        JSONArray jsonArray = new JSONArray();
        try {
            for (DetailedUser detailedUser :
                    list) {
                JSONObject jsonObject = new JSONObject();
                jsonUser(jsonObject, detailedUser);
                jsonArray.add(jsonObject);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonArray.toJSONString();
    }

    @Override
    public int setAccountStatus(String accountStatus, String account) {
        int count = -1;
        //查询状态
        String sqlSelectStatus = "select login_status from user_login where login_account= ?";
        String sql = "UPDATE user_login SET login_status = ? WHERE login_account = ?";
        String status = util.selectContent(sqlSelectStatus, new String[]{account});
        String[] param = {accountStatus, account};
        //正常状态
        if (Integer.parseInt(status) == 0) {
            //接收冻结 accountStatus=1
            if (Integer.parseInt(accountStatus) == 0) {
                param[0] = "1";
                count = DbUtil.updateContent(sql, param);
            }else {
                //已处于冻结
                return 0;
            }
            //冻结状态
        }else {
            //接收解除
            if (Integer.parseInt(accountStatus)==1){
                param[0]="0";
                count=DbUtil.updateContent(sql,param);
            }else {
                //已处于解除
                return 0;
            }
        }
        return count;
    }

    @Override
    public int AccountExist(String account) {
        String sql="SELECT COUNT(*) FROM user_login WHERE login_account= ?";
        return DbUtil.selectCountIsExist(sql,new String[]{account});
    }

    private void jsonUser(JSONObject json, DetailedUser user) {
        json.put("detailedUserAccount", user.getUser_account());
        json.put("detailedUserNickName", user.getNickname());
        json.put("detailedUserRemark", user.getRemark());
        json.put("detailedUserGender", user.getGender());
        json.put("detailedUserPhone", user.getPhone());
        json.put("detailedUserEmail", user.getEmail());
        json.put("detailedUserAddress", user.getAddress());
        json.put("detailedUserQuestionNumbers", user.getQuestion_numbers());
        json.put("detailedUserQuestionErrors", user.getQuestion_errors());
        json.put("detailedUserQuestionTrue", user.getQuestion_true());
        json.put("detailedUserQuestionAccuracyRate", user.getQuestion_accuracy_rate());
        json.put("login_id", user.getUserLogin().getLogin_id());
        json.put("login_password", user.getUserLogin().getLogin_password());
        json.put("login_status", user.getUserLogin().getLogin_status());
        json.put("login_identity", user.getUserLogin().getLogin_identity());
    }

}
